<?
include('inc_security.php');

//Khai báo biến khi hiển thị danh sách
$gs_title		= $module_name . ' | Danh sách';
$gs_action		= 'listing.php' . getURL(0,0,0,1,'record_id');
$gs_redirect	= 'listing.php' . getURL(0,0,0,1,'record_id');
$gs_errorMsg	= '';
$gs_alertMsg	= "";

/*****----- Quick Edit -----*****/
$action			= getValue("action", "str", "POST", "");
if($action == "execute"){
	
	//Get $record_id for edit data
	$arr_record_id	= getValue("record_id", "arr", "POST", "");
	if(is_array($arr_record_id)){
		
		//Loop array để update vào database
		for($i=0; $i<count($arr_record_id); $i++){
			
			$gs_errorMsg= "";
			
			//Lấy id của data cần sửa đổi
			$record_id	= intval($arr_record_id[$i]);
		
			//Lấy dữ liệu đề giữ nguyên trạng thái khi submit error
			$poll_name			= getValue("poll_name" . $record_id, "str", "POST", "");
			
			$myform = new generate_form();
			//Add table insert data
			$myform->addTable($gs_table);
			$myform->add("poll_name", "poll_name", 0, 1, " ", 1, "Bạn chưa nhập Tiêu đề cuộc thăm dò.", 0, "");
			
			//Check form data
			$gs_errorMsg .= $myform->checkdata();
			
			if($gs_errorMsg == ""){
			
				//Update to database
				$myform->removeHTML(0);
				$db_update = new db_execute($myform->generate_update_SQL($id_field, $record_id));
				unset($db_update);
				
			}//End if($gs_errorMsg == "")
			else{
				$gs_alertMsg .= "- Bạn không thay đổi được dữ liệu có ID = " . $record_id . ".\\n";
			}
			
			unset($myform);
			
		}//End for($i=0; $i<count($arr_record_id); $i++)
		
		//Nếu có data nào bị lỗi thì hiển thị thông báo alert message error rồi redirect
		if($gs_alertMsg != ""){
			echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
			echo '<script language="javascript">alert("Có những lỗi sau:\\n' . $gs_alertMsg . '"); window.location.href="' . $gs_redirect . '"</script>';
			exit();
		}
		//Ngược lại thì redirect luôn
		else redirect($gs_redirect);
		
	}//End if(is_array($arr_record_id))
	
}
/*****----- End Quick Edit -----*****/

$record_id	= getValue("record_id");

//Search data
$id			= getValue("id");
$keyword		= getValue("keyword", "str", "GET", "", 1);
$sqlWhere	= "";
//Tìm theo ID
if($id > 0)	$sqlWhere .= " AND poll_id = " . $id . " ";
//Tìm theo keyword
if($keyword != ""){
	$sqlWhere	.= " AND (poll_name LIKE '%" . $keyword . "%') ";
}

//Sort data
$sort			= getValue("sort");
switch($sort){
	case 1:$sqlOrderBy = "poll_name ASC"; break;
	case 2:$sqlOrderBy = "poll_name DESC"; break;
	default:$sqlOrderBy = "poll_name ASC"; break;
}

//Get page break params
$page_size		= 20;
$page_prefix	= "Trang: ";
$normal_class	= "page";
$selected_class= "page_current";
$previous		= "<";
$next				= ">";
$first			= "<<";
$last				= ">>";
$break_type		= 1;//"1 => << < 1 2 [3] 4 5 > >>", "2 => < 1 2 [3] 4 5 >", "3 => 1 2 [3] 4 5", "4 => < >"
$url				= getURL(0,0,1,1,"page");
$db_count		= new db_query("SELECT COUNT(*) AS count
										 FROM " . $gs_table . "
										 WHERE " . $id_field . " <> " . $record_id . " AND lang_id = " . $lang_id . $sqlWhere);
$listing_count	= mysql_fetch_array($db_count->result);
$total_record	= $listing_count["count"];
$current_page	= getValue("page", "int", "GET", 1);
if($total_record % $page_size == 0) $num_of_page = $total_record / $page_size;
else $num_of_page = (int)($total_record / $page_size) + 1;
if($current_page > $num_of_page) $current_page = $num_of_page;
if($current_page < 1) $current_page = 1;
$db_count->close();
unset($db_count);
//End get page break params
$db_listing	= new db_query("SELECT *
									 FROM " . $gs_table . "
									 WHERE " . $id_field . " <> " . $record_id . " AND lang_id = " . $lang_id . $sqlWhere . "
									 ORDER BY " . $sqlOrderBy . "
									 LIMIT " . ($current_page-1) * $page_size . "," . $page_size);
?>
<html>
<head>
<title><?=$gs_title?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css" media="all">@import "../css/GSportal.css";</style>
<script language="javascript" src="../js/library.js"></script>
<script language="javascript" src="../js/tooltip.js"></script>
<script language="javascript" src="../js/AC_RunActiveContent.js"></script>
</head>
<body>
<div class="bg_title_content">
<div class="content_title" style="float:left"><?=$gs_title?>: <font class="count"><?=format_number($total_record)?></font></div>
<div class="content_title" style="float:right"><a title="Thêm mới" href="add.php?type=<?=$type?>"><img align="absmiddle" border="0" hspace="5" src="<?=$gs_imagepath?>add.gif" />Thêm mới</a></div>
</div>
<div align="center" class="content">
<? //Page break and search data?>
<table width="98%" cellpadding="2" cellspacing="2">
	<tr>
	<?	if($total_record > $page_size){?>
		<td nowrap="nowrap"><?=generatePageBar($page_prefix, $current_page, $page_size, $total_record, $url, $normal_class, $selected_class, $previous, $next, $first, $last, $break_type)?></td>
	<? }?>
		<td align="right">
			<table cellpadding="0" cellspacing="0">
			<form name="search" action="<?=getURL(0,0,1,0)?>" method="get">
				<tr>
					<td class="form_search" nowrap="nowrap">
						ID:
						<input title="ID" type="text" class="form_control" id="id" name="id" value="<?=$id?>" maxlength="11" style="width:60px; text-align:right">&nbsp;
						Từ khóa:
						<input title="Từ khóa" type="text" class="form_control" id="keyword" name="keyword" value="<?=htmlspecialbo($keyword)?>" maxlength="255" style="width:100px">&nbsp;
						<input type="hidden" name="sort" value="<?=$sort?>" />
					</td>
					<td class="form_search" style="padding-left:5px"><input title="Tìm kiếm" type="image" src="<?=$gs_imagepath?>search.gif" border="0"></td>
				</tr>
			</form>
			</table>
		</td>
	</tr>
</table>
<? //End page break and search data?>
<table class="table" border="1" bordercolor="#e5e3e6" cellpadding="3" cellspacing="0" width="98%">
	<tr class="table_title_3">
		<td>Stt.</td>
		<td>
			<div>Tiêu đề cuộc thăm dò</div>
			<div>
				<?=generate_sort("asc", 1, $sort, $gs_imagepath)?>
				<?=generate_sort("desc", 2, $sort, $gs_imagepath)?>
			</div>
		</td>
        <td>Số phiếu</td>
		<td>Multi</td>
		<td>Kích<br />hoạt</td>
		<td>Lưu</td>
        <td>Sửa</td>
        <td>Xóa</td>
	</tr>
<?
//Call class form
$form = new form();
$form->class_form_name = "form_name_2";
?>
<?
$ban_module	= 0;
$ban_type	= 0;
$record_id	= getValue("record_id", "int", "POST");
//Đếm số thứ tự
$No = ($current_page - 1) * $page_size;
while($listing = mysql_fetch_array($db_listing->result)){
	$No++;
?>
	<?
	$form->create_form("quick_edit_" . $No, $gs_action, "post", "multipart/form-data");
	?>
	<tr id="tr_<?=$No?>" <?=$gs_change_bg?>>
		<td class="No"><?=$No?></td>
		<td align="center">
			<?=$form->create_table(2, 2, "");?>
			<?=$form->text("Tên", "poll_name_" . $No, "poll_name" . $listing["poll_id"], $listing["poll_name"], "Tiêu đề cuộc thăm dò", 1, 175, "", 255, "", "", "")?>
			<?=$form->close_table();?>
		</td>
        <td align="center"><span class="text_link_bold"><?=$listing['poll_opt_num']?></span></td>
        <td align="center"><a href="quickset.php?type=multi&record_id=<?=$listing["poll_id"]?>&redirect=<?=base64_encode(getURL())?>"><img border="0" src="<?=$gs_imagepath?>check_small_<?=$listing["poll_multi"]?>.gif" /></a></td>
		<td align="center"><a href="quickset.php?type=active&record_id=<?=$listing["poll_id"]?>&redirect=<?=base64_encode(getURL())?>"><img border="0" src="<?=$gs_imagepath?>active_<?=$listing["poll_active"]?>.gif" /></a></td>
		<td align="center">
        	<input title="Lưu dữ liệu" type="image" hspace="5" src="<?=$gs_imagepath?>save.gif" onClick="MM_validateForm('poll_name_<?=$No?>','','R'); return document.MM_returnValue" />
		</td>
        <td align="center">
            <a title="Sửa dữ liệu" href="edit.php?record_id=<?=$listing["poll_id"]?>&redirect=<?=base64_encode(getURL())?>"><img border="0" hspace="5" src="<?=$gs_imagepath?>edit.gif"></a>
		</td>
		<td align="center">
            <img title="Xóa dữ liệu" hspace="5" src="<?=$gs_imagepath?>delete.gif" style="cursor:pointer" onClick="if(confirm('Bạn có muốn xóa dữ liệu này không?')){window.location.href='delete.php?record_id=<?=$listing["poll_id"]?>&redirect=<?=base64_encode(getURL())?>'}" />
		</td>
	</tr>
	<?=$form->hidden("record_id_" . $No, "record_id[]", $listing['poll_id'], "");?>
<?
}// End while($listing = mysql_fetch_array($db_listing->result))
?>
	<?=$form->hidden("action", "action", "execute", "");?>
<?
$form->close_form();
unset($form);
?>
</table>
</div>
</body>
</html>
<script language="javascript">ButtonLeftFrame();</script>